//=============================================================================
// TF Medivac. Structure for healing players.
//=============================================================================

class TFMedivac extends TFStation
	placeable;

state Ready
{
    function BeginState()
    {
        super.BeginState();

        tfgri(level.game.GameReplicationInfo).TeamCredits[myteam]+=10;

        if(myteam==0) setstaticmesh(StaticMesh'TFMedia.items.tfMedivacRed');
        else setstaticmesh(StaticMesh'TFMedia.items.tfMedivacBlu');

        settimer(1,true);
    }

    function Timer()
    {
        local float amount, amount2;
//        local int count;
        local bool bplaysound;
        local float shieldmax, healthmax;
        local xemitter gfx;

        FindNear();

        if(near==none) return;

        if(vsize(location-near.location)>80 || near.Controller==none || near.PlayerReplicationInfo.Team.TeamIndex!=myteam)
        {
             near=none;
             return;
        }

        if(tfpri(near.PlayerReplicationInfo).spawnprotected)
            tfpri(near.PlayerReplicationInfo).spawnprotected=false;

        if(grade==3) shieldmax=199;
        else if(grade==2) shieldmax=100;

        amount=fmax(0,fmin(shieldmax-near.ShieldStrength,5));
        if(amount>=1)
        {
            near.AddShieldStrength(amount);
            PlaySound(sound'TFMedia.TFRepArmor',SLOT_None,0.6);
            gfx=spawn(class'Taskforces.tfMedivacFX',,,near.Location);
            gfx.mSpawnVecA=Location;
        }

        if(grade==3) healthmax=199;
        else if(grade==2) healthmax=150;
        else if(grade==1) healthmax=120;

        amount2=fmax(0,fmin(healthmax-near.Health,5));
        if(amount2>=1)
        {
            near.GiveHealth(amount2,healthmax);
            PlaySound(Sound'TFMedia.tfMDFire',SLOT_None,0.6);
            near.Spawn(class'Taskforces.TFShieldHealFX');
        }

        bplaysound=false;
    }
}

function Destructed(pawn killer, bool recycled)
{
    tfgri(level.game.GameReplicationInfo).TeamCredits[myteam]-=10;

    super.Destructed(killer, recycled);
}

defaultproperties
{
     BuildTime=60.000000
     MaxHealth=1000.000000
     chunkcount=5
     BuildingName="Medivac"
     NameIndex=7
     teamflareDS=0.900000
     StaticMesh=StaticMesh'TFMedia.items.tfMedivac'
     AmbientSound=sound'TFMedia.Buildings.tfArmoryAmb'
     DrawScale=0.7500000
     bCollideWorld=True
     Physics=PHYS_Falling
     CollisionRadius=40.000000
     CollisionHeight=55.000000
}
